package session.stateless.SCM;

import entity.SCM.MaterialLine;
import entity.RawMaterial;
import entity.SCM.Supplier;
import entity.SCM.SupplyItem;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class SupplierBean implements SupplierBeanLocal {

    @PersistenceContext()
    EntityManager em;

    private SupplyItem createSupplyItem(String type, Long price) {
        SupplyItem supplyItem = new SupplyItem();
        supplyItem.setSupplyItemPrice(price);
        supplyItem.setSupplyItemType(type);
        supplyItem.setSupplier(null);
        em.persist(supplyItem);
        em.flush();
        return supplyItem;
    }

    @Override
    public boolean deleteSupplyItem(Long supplyItemId) {
        SupplyItem si = em.find(SupplyItem.class, supplyItemId);
        if (si == null) {
            System.out.print("Cannot find the supply item");
            return false;
        } else {
            si.getSupplier().getSupplyItemList().remove(si);
            si.setSupplier(null);
            em.remove(si);
            em.flush();
            return true;
        }
    }

    @Override
    public String createSupplier(String supplierName, String supplierAddress, String supplierPostCode, String supplierContactNo,
            String supplierEmail, List<SupplyItem> supplyItemList) {
        Supplier supplier = new Supplier();
        supplier.setSupplierState(Supplier.State.exist);
        supplier.setSupplierContactNo(supplierContactNo);
        supplier.setSupplierAddress(supplierAddress);
        supplier.setSupplierPostcode(supplierPostCode);
        supplier.setSupplierName(supplierName);
        supplier.setSupplierEmail(supplierEmail);
        List<SupplyItem> tempList = new ArrayList<SupplyItem>();
        if (supplyItemList != null) {
            for (SupplyItem si : supplyItemList) {
                SupplyItem temp = this.createSupplyItem(si.getSupplyItemType(), si.getSupplyItemPrice());
                tempList.add(temp);
            }
        }

        supplier.setSupplyItemList(tempList);
        em.persist(supplier);
        em.flush();

        for (SupplyItem temp : supplier.getSupplyItemList()) {
            temp.setSupplier(supplier);
            em.flush();
        }

        return supplier.getSupplierName();
    }

    @Override
    public boolean updateSupplier(String supplierName, String supplierAddress, String supplierPostCode, String supplierContactNo,
            String supplierEmail, List<SupplyItem> supplyItemList) {
        Supplier supplier = em.find(Supplier.class, supplierName);
        if (supplier == null) {
            return false;
        } else {
            supplier.setSupplierContactNo(supplierContactNo);
            supplier.setSupplierAddress(supplierAddress);
            supplier.setSupplierEmail(supplierEmail);
            supplier.setSupplierPostcode(supplierPostCode);

            List<SupplyItem> siList = supplier.getSupplyItemList();
            supplier.setSupplyItemList(null);
            for (SupplyItem si : siList) {
                si.setSupplier(null);
                em.remove(si);
                em.flush();
            }

            List<SupplyItem> tempList = new ArrayList<SupplyItem>();
            for (SupplyItem si : supplyItemList) {
                SupplyItem temp = new SupplyItem();
                if (si.getSupplyItemPrice() == null || si.getSupplyItemPrice().equals(new Long(0))) {
                    supplyItemList.remove(si);
                    si.setSupplier(null);
                    em.remove(si);
                    em.flush();
                } else {
                    temp = this.createSupplyItem(si.getSupplyItemType(), si.getSupplyItemPrice());
                    temp.setSupplier(supplier);
                    tempList.add(temp);
                }
            }

            supplier.setSupplyItemList(tempList);
            em.flush();

            return true;
        }
    }

    @Override
    public boolean deleteSupplier(String supplierName) {
        Supplier supplier = em.find(Supplier.class, supplierName);
        if (supplier == null) {
            System.out.print("Cannot find supplier when delete supplier");
            return false;
        } else {
            supplier.setSupplierState(Supplier.State.delete);
            em.flush();
            return true;
        }
    }

    @Override
    public Supplier getSupplier(String name) {
        return em.find(Supplier.class, name);
    }

    @Override
    public List<Supplier> getSupplierForMaterial(String materialName) {
        Query q = em.createQuery("SELECT c FROM SupplyItem c WHERE c.supplyItemType = :supplyItemType");
        q.setParameter("supplyItemType", materialName);
        List<SupplyItem> supplyItemList = q.getResultList();
        List<Supplier> temp = new ArrayList<Supplier>();
        for (SupplyItem si : supplyItemList) {
            if (!si.getSupplier().getSupplierState().equals(Supplier.State.delete)) {
                temp.add(si.getSupplier());
            }
        }
        return temp;
    }

    @Override
    public List<Supplier> getAllSupplier() {
        Query q = em.createQuery("SELECT c FROM Supplier c WHERE c.supplierState = :state");
        q.setParameter("state", Supplier.State.exist);
        return q.getResultList();
    }

    @Override
    public List<RawMaterial> getAllMaterial() {
        Query q = em.createQuery("SELECT c FROM RawMaterial c");
        return q.getResultList();
    }

    @Override
    public List<Supplier> getSupplierForOrder(List<MaterialLine> mls) {
        List<Supplier> suppliers = this.getAllSupplier();
        List<Supplier> temp = new ArrayList<Supplier>();
        int no = suppliers.size();
        for (int i = 0; i < no; i++) {
            int count = 0;
            System.out.print("-----------here1------------------" + suppliers.get(i).getSupplyItemList().size() + "-----"+mls.size());
            
            if (suppliers.get(i).getSupplyItemList().size() >= mls.size()) {
                for (int j = 0; j < mls.size(); j++) {
                    System.out.print("-----------here2------------------");
                    for (int m = 0; m < suppliers.get(i).getSupplyItemList().size(); m++) {
                        if (mls.get(j).getRawMaterial().getMaterialType().equals(suppliers.get(i).getSupplyItemList().get(m).getSupplyItemType())) {
                            count++;
                            System.out.print("-----------here3------------------" + count);
                            System.out.print(mls.get(j).getRawMaterial().getMaterialType() + 
                                    "^^^^^" + suppliers.get(i).getSupplyItemList().get(m).getSupplyItemType());
                            break;
                        }
                    }
                    if(count==0){
                        break;
                    }
                }
            }
            if (count == mls.size()) {
                System.out.print("-----------here4------------------");
                temp.add(suppliers.get(i));
            }
        }
        
        System.out.print("------------"+temp.size());
        return temp;
    }

    public Supplier findSupplierByName(String supplierName)
    {
        Supplier temp = em.find(Supplier.class, supplierName);
        return temp;
    }
}